﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;
using PsychveyApp1.classes;

namespace PsychveyApp1.classes
{
    public class OpenEndedQuestionPanel : System.Web.UI.WebControls.Panel
    {
        public TextBox tbxQuestion;
        public TextBox tbxAnswer;
        public RadioButtonList rblQuestionRadioButtonList;
        public Button btnQuestionButton;
        public Button btnAddAnswer;
        public Button btnRemoveAnswer;
        public Button btnEditAnswer;
        public Button btnDeleteQuestion;
        public Button btnSaveAnswer;
        public Button btnClear;
        public DropDownList dropDownListOE;
        public int totalNoOfQuestions;
        public bool currentEditing = false;

        public Table tableMain;

        public Survey currentSurvey;
        public UserAccount currentUser;
        public Question currentQuestion;
        public Answer openEndedAnswer;

        public ArrayList listOfAnswers;
        public ArrayList listOfQuestions;

        // Default public constructor 
        public OpenEndedQuestionPanel(UserAccount user, Survey survey, Question question, ArrayList listOfAnswers, int totalNoQuestions, ArrayList listOfQuestions)
            : base()
        {
            // pass value from outside
            currentUser = user;
            currentSurvey = survey;
            currentQuestion = question;
            this.listOfAnswers = listOfAnswers;
            this.totalNoOfQuestions = totalNoQuestions;
            this.listOfQuestions = listOfQuestions;



            tableMain = new Table();

            //--------------------------
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            Label label = new Label();
            cell.ColumnSpan = 2;
            label.Text = "Question No";
            cell.Controls.Add(label);
            row.Cells.Add(cell);

            dropDownListOE = new DropDownList();
            for (int i = 0; i < this.totalNoOfQuestions; i++)
            {
                dropDownListOE.Items.Add(Convert.ToString(i + 1));
            }
            dropDownListOE.SelectedValue = question.questionOrder;
            dropDownListOE.AutoPostBack = true;
            cell.Controls.Add(dropDownListOE);
            //dropdownList.ID = "ddlQuestionNo";
            //dropdownList.Items.Insert(0, new ListItem("QNo", "QNo"));
            cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
            row.Cells.Add(cell);

            //cell = new TableCell();
            //btnEditAnswer = new Button();
            //btnEditAnswer.Text = "Edit Question";
            //cell.Controls.Add(btnEditAnswer);
            //cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
            //row.Cells.Add(cell);


            cell = new TableCell();
            btnDeleteQuestion = new Button();
            btnDeleteQuestion.Text = "Delete Question";
            btnDeleteQuestion.CausesValidation = false;
            cell.Controls.Add(btnDeleteQuestion);
            row.Cells.Add(cell);
            cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
            tableMain.Rows.Add(row);

            //-----------------------
            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.Text = "Question Type: " + question.typeOfQuestion;
            cell.ColumnSpan = 2;
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            tableMain.Rows.Add(row);
            //-----------------------

            row = new TableRow();
            cell = new TableCell();
            cell.ColumnSpan = 4;
            tbxQuestion = new TextBox();
            tbxQuestion.Text = question.questionsAsked.ToString();
            tbxQuestion.Width = 500;
            cell.Controls.Add(tbxQuestion);
            row.Cells.Add(cell);
            tableMain.Rows.Add(row);
            this.Controls.Add(tableMain);


            //-----second table ------------
            Table secondTable = new Table();
            row = new TableRow();
            cell = new TableCell();
            btnSaveAnswer = new Button();
            btnSaveAnswer.Text = "Save";
            btnSaveAnswer.CausesValidation = false;
            cell.Controls.Add(btnSaveAnswer);
            row.Cells.Add(cell);

            //cell = new TableCell();
            btnClear = new Button();
            btnClear.Text = "Clear";
            cell.Controls.Add(btnClear);
            btnClear.CausesValidation = false;
            row.Cells.Add(cell);
            secondTable.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();
            cell.ColumnSpan = 2;
            Image breakLine = new Image();
            breakLine.ImageUrl = ("~/images/breakline.png");
            cell.Controls.Add(breakLine);
            row.Controls.Add(cell);
            secondTable.Rows.Add(row);

            this.Controls.Add(secondTable);

            //currentQuestion = new Question();
            //currentQuestion.questionID = currentUser.username + "__" + currentSurvey.title + "__" + currentQuestion.questionsAsked;
            //currentQuestion.typeOfQuestion = "";
            //currentQuestion.surveyID = currentUser.userID + "__" + currentSurvey.title;
            //currentQuestion.questionsAsked = "";
            //currentQuestion.userID = currentUser.userID;
            //DBManager.InsertQuestion(currentQuestion, currentSurvey, currentUser);
        }



        public void SaveAnswer(object sender, EventArgs e)
        {

            DBManager.UpdateQuestion(currentUser.userID, currentSurvey.surveyID,
                currentQuestion.questionID, tbxQuestion.Text);

        }

        public void RemoveQuestion(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listOfAnswers.Count; i++)
            {
                DBManager.DeleteAnswer(((Answer)this.listOfAnswers[i]).answerID, currentQuestion.questionID, currentSurvey.surveyID, currentUser.userID);
                //tbxQuestion.Text = ((Answer)this.listOfQuestions[i]).answerText;

            }
            DBManager.DeleteQuestion(currentUser.userID, currentSurvey.surveyID,
                   currentQuestion.questionID);
            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID,
                  currentQuestion.questionID);
            int x = Convert.ToInt32(currentQuestion.questionOrder);
            for (int i = 0; i < this.totalNoOfQuestions; i++)
            {

                if ((Convert.ToInt32(((Question)listOfQuestions[i]).questionOrder) > x))
                {
                    DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[i]).questionID, Convert.ToString(Convert.ToInt32(((Question)listOfQuestions[i]).questionOrder) - 1));
                }
            }
            HttpContext.Current.Response.Redirect("CreateSurvey2.aspx");
          
        }


        public void ClearForm(object sender, EventArgs e)
        {

            tbxQuestion.Text = "";

        }

        public void UpdateQuestionOrder(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(currentQuestion.questionOrder);
            int y = Convert.ToInt32(dropDownListOE.SelectedItem.Text);
                if (x > y)
                {
                    for (int r = 0; r < this.listOfQuestions.Count; r++)
                    {
                        if ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) < x) && ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) >= y)))
                        {
                            DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[r]).questionID, Convert.ToString(Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) + 1));
                        }

                    }
                }
                else if (x < y)
                {
                    for (int r = 0; r < this.listOfQuestions.Count; r++)
                    {
                        if ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) > x) && ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) <= y)))
                        {
                            DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[r]).questionID, Convert.ToString(Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) - 1));
                        }
                    }
                }

            DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, currentQuestion.questionID, dropDownListOE.SelectedItem.Text);

            HttpContext.Current.Response.Redirect("CreateSurvey2.aspx");
        }
    }
}